{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sisl\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example will setup the required electronic structures for usage in TBtrans.  \n",
    "We will continue with the graphene nearest neighbour tight-binding model and perform simple transport calculations using TBtrans.  \n",
    "Again we require the graphene unit-cell and the construction of the Hamiltonian object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "graphene = sisl.geom.graphene().tile(2, axis=0)\n",
    "H = sisl.Hamiltonian(graphene)\n",
    "H.construct([[0.1, 1.43], [0., -2.7]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the above call of the graphene lattice is different from [TB 2](../TB_02/run.ipynb), and similar to [TB 1](../TB_01/run.ipynb). In this example, we will create a *non-orthogonal* graphene lattice, i.e. the lattice vectors are the minimal lattice vectors of graphene.\n",
    "The minimal graphene lattice consists of 2 Carbon atoms.\n",
    "We `tile` the `Geometry` to make it slightly bigger.  \n",
    "You are encouraged to draw the graphene lattice vectors, and draw an arrow in the direction of the transport (along the 2nd lattice vector). Note that one *can* calculate transport along non-orthogonal directions (also in TranSiesta).\n",
    "\n",
    "Assert that we have 16 non-zero elements:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(H)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Hamiltonian we have thus far created will be our *electrode*. Let's write it to a TBtrans readable file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "H.write('ELEC.nc')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now a file `ELEC.nc` file exists in the folder, and it contains all the information (and more) that TBtrans requires to construct the self-energies for the electrode."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating the device, `Hamiltonian` $\\to$ `Hamiltonian`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `Geometry.tile` function is an explicit method to create bigger lattices from a smaller reference lattice. However, the `tile` routine is also available to the `Hamiltonian` object. Not only is it much easier to use, it also presents these advantages:\n",
    "\n",
    "* It guarantees that the matrix elements are the same as the reference `Hamiltonian`, i.e. you need not specify the parameters to `construct` twice,\n",
    "* It is *much* faster when creating systems of $>500,000$ atoms/orbitals from smaller reference systems,\n",
    "* It also requires less code which increases readability and is less prone to errors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "H_device = H.tile(3, axis=1)\n",
    "print(H_device)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For more information you may execute the following lines to view the documentation:\n",
    "\n",
    "    help(Geometry.tile)\n",
    "    help(Hamiltonian.tile)\n",
    "\n",
    "\n",
    "Now we have created the device electronic structure. The final step is to store it in a TBtrans readable format:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "H_device.write('DEVICE.nc')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now run tbtrans:\n",
    "\n",
    "    tbtrans RUN.fdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tbt = sisl.get_sile('siesta.TBT.nc')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After calculating the transport properties of the transport problem, you may also use `sisl` to interact with the TBtrans output (in the `*.TBT.nc` file). Please repeat the same convergence tests you performed in example [TB 2](../TB_02/run.ipynb).  \n",
    "What are the required **k**-point sampling compared to [TB_02](../TB_02/run.ipynb) for a similar transmission function?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(tbt.E, tbt.transmission(), label='k-averaged'); \n",
    "plt.plot(tbt.E, tbt.transmission(kavg=tbt.kindex([0, 0, 0])), label=r'$\\Gamma$'); \n",
    "plt.xlabel('Energy [eV]'); plt.ylabel('Transmission'); plt.ylim([0, None]) ; plt.legend();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercises\n",
    "\n",
    "- Extract the DOS for each sub-lattice and plot them, see the 1) `atoms` or 2) `orbitals` keywords in the `.DOS` and `.ADOS` routines"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "source": [
    "### Learned objectives\n",
    "\n",
    "- Calculation of transport in non-orthogonal lattices are possible, this applies to both TBtrans *and* TranSiesta\n",
    "- Extraction of DOS for a subset of atoms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
